<?php
$ROOT_DIR_PATH = '../';
session_start();
require_once($ROOT_DIR_PATH.'includes/includes.php');
require_once($ROOT_DIR_PATH.'classes/class.user.inc.php');
$GENOBJ->clearSessionErrorMessages();
$redirectUrl = $ADMIN_BASE_HREF . "login.php";
$email = '';
if ($_POST) {
    /* created the object of the class user */
    $userObj = new user();
    /* filtering the username and password to prevent sql injection */
    $filterArray = Array("TrimElement", "mysqlRealEscape");
    $email = $_POST['email'];
    $email = $GENOBJ->filter($email, $filterArray);
    if ($email != '') {
        if (!$GENOBJ->ValidEmailAddress($email)) {
            $_SESSION['error']['forgetpassword-emailerror'] = 'Email address is not valid';
            $GENOBJ->redirect($redirectUrl);
            exit;
        }
        $user = $userObj->checkEmailExists($email);
        if ($user) {
            $username = $user['email'];
            $password = base64_decode($user['password']);
            $to = $email;
            $subject = 'Password of adminpanel of '.$companyName;
            $message = "
				<html>
				<head>
				</head>
				<body>
				<p>Password of $companyName admin!</p>
				<table border='1'>
					<tr>
						<th>Email</th>
						<th>Password</th>
					</tr>
					<tr>
						<td>$username</td>
						<td>$password</td>
					</tr>
				</table>
				</body>
				</html>
				";
            $headers = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            // Additional headers
            $headers .= "To: $username <$to>" . "\r\n";
            $headers .= "From: $companyName admin <$admin_email>" . '\r\n';

            $success = mail($to, $subject, $message, $headers);
            /* email code over here */
            $_SESSION['error']['forgetpasswordsend'] = 'Password send to your e-mail';
            $GENOBJ->redirect($redirectUrl);
            exit;
        } else {
            $_SESSION['error']['forgetpassword-emailerror'] = 'Email-Address doesnot exist';
            $GENOBJ->redirect($redirectUrl);
            exit;
        }
    } else {
        $_SESSION['error']['forgetpassword-emailerror'] = 'Email-Address shouldnot be blank';
        $GENOBJ->redirect($redirectUrl);
        exit;
    }
}
?>